﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    /// <summary>
    /// 设备管理
    /// </summary>
    public class DeviceRepository: BaseRepository<TDevice>, IDeviceRepository
    {
        public DeviceRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public bool AddDevice(DeviceDto dto)
        {
            try
            {
                string code = "Device";
                var aobj = Context.TDevices.AsNoTracking().Where(x => x.Code.Contains(code)).OrderByDescending(x => x.CreateDate).FirstOrDefault();
                if (aobj != null)
                {
                    string pattern = @"\d+";
                    MatchCollection matches = Regex.Matches(aobj.Code, pattern);
                    string value = matches[matches.Count - 1].Value;
                    int values = Convert.ToInt32(value);
                    values++;
                    dto.Code = code + values.ToString("D4");
                }
                else
                {
                    int i = 1;
                    dto.Code = code + i.ToString("D4");
                }

                var obj = Context.TDevices.AsNoTracking().FirstOrDefault(x => x.Code == dto.Code);
                if(obj!=null)
                {
                    return false;
                }
                TDevice dev = new TDevice()
                {
                    Code=dto.Code,
                    Name=dto.Name,
                    DeviceType=dto.DeviceType,
                    Status=dto.Status,
                    ExceptionMsg=dto.ExceptionMsg,
                    ExceptionDate=dto.ExceptionDate,
                    WarehouseCode=dto.WarehouseCode,
                    WarehouseId=dto.WarehouseId,
                    WarehouseName=dto.WarehouseName,
                    CreateDate = DateTime.Now,
                    CreateUserId = dto.NowUserId,
                    CreateUserName = dto.NowUserRealName
                };

                Context.TDevices.Add(dev);
                return Context.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public bool UpdateDevice(DeviceDto dto)
        {
            try
            {
                var obj = Context.TDevices.AsNoTracking().FirstOrDefault(x => x.Id != dto.Id && x.Code == dto.Code);
                if(obj!=null)
                {
                    return false;
                }
                TDevice dev = new TDevice()
                {
                    Id=dto.Id,
                    Code = dto.Code,
                    Name = dto.Name,
                    DeviceType = dto.DeviceType,
                    Status = dto.Status,
                    ExceptionMsg = dto.ExceptionMsg,
                    ExceptionDate = dto.ExceptionDate,
                    WarehouseCode = dto.WarehouseCode,
                    WarehouseId = dto.WarehouseId,
                    WarehouseName = dto.WarehouseName,
                    ModifyDate = DateTime.Now,
                    ModifyUserId = dto.NowUserId,
                    ModifyUserName = dto.NowUserRealName,
                    CreateDate = dto.CreateDate,
                    CreateUserId = dto.NowUserId,
                    CreateUserName = dto.NowUserRealName
                };
                Context.TDevices.Update(dev);
                return Context.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DelDevice(int ids)
        {
            try
            {
                var carr = Context.TDevices.Find(ids);
                Context.TDevices.Remove(carr);
                return Context.SaveChanges() > 0;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 设备分页查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<TDevice> GetDevices(string house, string name, int pageIndex, int pageSize, out int totalCount)
        {
            try
            {
                var list = Context.TDevices.AsQueryable();
                if (!string.IsNullOrEmpty(house))
                {
                    list = list.Where(x=>x.WarehouseName.Contains(house));
                }
                if (!string.IsNullOrEmpty(name))
                {
                    list = list.Where(x => x.Name.Contains(name));
                }
                totalCount = list.Count();
                list = list.OrderByDescending(x => x.CreateDate).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                
                return list.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public TDevice GetDevice(int id)
        {
            return Context.TDevices.Find(id);
        }
    }
}
